﻿using System;
using Core.CSV;
using Core.UI;
using Game.UI;
using Game.UI.Weapon;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Game.View {
    public class WeaponEffectItem : DraggableItem,IPointerEnterHandler,IPointerExitHandler {
        private WeaponEffectSlot mPrevSlot;
        private WeaponEffectSlot mCurSlot;
        
        public TBWeaponEffectConfig Data {
            get;
            private set;
        }
        
        private bool mIsDragging = false;
        
        public void Init(uint effectID) {
            Data = Tables.TBWeaponEffect.GetRow<TBWeaponEffectConfig>(effectID);
            transform.localPosition = Vector3.zero;
        }
            
            
        public override void OnBeginDrag(PointerEventData eventData) {
            mIsDragging = true;
            UIMgr.Ins.CloseWindow<TipsWindow>();
            WeaponEffectSlot slot = MouseInputUtils.GetFirstComponentFromUI<WeaponEffectSlot>();
            mPrevSlot = slot;
        }

        public override void OnEndDrag(PointerEventData eventData) {
            mIsDragging = false;
            WeaponEffectSlot slot = MouseInputUtils.GetFirstComponentFromUI<WeaponEffectSlot>();
            if (slot != null) {
                mCurSlot = slot;
                mPrevSlot.UpdateItem(mCurSlot.Item);
                mCurSlot.UpdateItem(this);
            } else {
                transform.localPosition = Vector3.zero;
            }
        }

        public void OnPointerEnter(PointerEventData eventData) {
            if (mIsDragging) {
                return;
            }
            if (Data != null) {
                UIMgr.Ins.OpenWindow<TipsWindow>(Data.Description);
            } else {
                UIMgr.Ins.OpenWindow<TipsWindow>("无");
            }
        }

        public void OnPointerExit(PointerEventData eventData) {
            if (mIsDragging) {
                return;
            }
            UIMgr.Ins.CloseWindow<TipsWindow>();
        }
    }
}